package com.digicom.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.digicom.common.core.domain.ApiResponse;
import com.digicom.common.core.utils.SecurityUtils;
import com.digicom.common.entity.Interaction;
import com.digicom.service.interaction.InteractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 交互接口控制器
 */
@RestController
@RequestMapping("/api/interaction")
public class InteractionController {

    @Autowired
    private InteractionService interactionService;

    /**
     * 点赞文章
     */
    @PostMapping("/like/{articleId}")
    public ApiResponse<Boolean> like(@PathVariable Long articleId, 
                                    @RequestParam(required = false, defaultValue = "web") String clientType) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 点赞文章
        boolean result = interactionService.likeArticle(userId, articleId, clientType);
        return ApiResponse.success(result);
    }

    /**
     * 取消点赞
     */
    @DeleteMapping("/like/{articleId}")
    public ApiResponse<Boolean> cancelLike(@PathVariable Long articleId) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 取消点赞
        boolean result = interactionService.cancelLike(userId, articleId);
        return ApiResponse.success(result);
    }

    /**
     * 收藏文章
     */
    @PostMapping("/collect/{articleId}")
    public ApiResponse<Boolean> collect(@PathVariable Long articleId, 
                                      @RequestParam(required = false, defaultValue = "web") String clientType) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 收藏文章
        boolean result = interactionService.collectArticle(userId, articleId, clientType);
        return ApiResponse.success(result);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/collect/{articleId}")
    public ApiResponse<Boolean> cancelCollect(@PathVariable Long articleId) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 取消收藏
        boolean result = interactionService.cancelCollect(userId, articleId);
        return ApiResponse.success(result);
    }

    /**
     * 分享文章
     */
    @PostMapping("/share/{articleId}")
    public ApiResponse<Boolean> share(@PathVariable Long articleId, 
                                    @RequestParam(required = false, defaultValue = "web") String clientType) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 分享文章
        boolean result = interactionService.shareArticle(userId, articleId, clientType);
        return ApiResponse.success(result);
    }

    /**
     * 获取我点赞的文章列表
     */
    @GetMapping("/liked")
    public ApiResponse<Page<Interaction>> liked(@RequestParam(defaultValue = "1") Integer pageNum,
                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 获取用户点赞的文章列表
        Page<Interaction> page = new Page<>(pageNum, pageSize);
        Page<Interaction> result = interactionService.listUserLiked(userId, page);
        return ApiResponse.success(result);
    }

    /**
     * 获取我收藏的文章列表
     */
    @GetMapping("/collected")
    public ApiResponse<Page<Interaction>> collected(@RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 获取用户收藏的文章列表
        Page<Interaction> page = new Page<>(pageNum, pageSize);
        Page<Interaction> result = interactionService.listUserCollected(userId, page);
        return ApiResponse.success(result);
    }

    /**
     * 检查用户是否点赞文章
     */
    @GetMapping("/check/like/{articleId}")
    public ApiResponse<Boolean> checkLiked(@PathVariable Long articleId) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 检查用户是否点赞文章
        boolean result = interactionService.checkUserLiked(userId, articleId);
        return ApiResponse.success(result);
    }

    /**
     * 检查用户是否收藏文章
     */
    @GetMapping("/check/collect/{articleId}")
    public ApiResponse<Boolean> checkCollected(@PathVariable Long articleId) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 检查用户是否收藏文章
        boolean result = interactionService.checkUserCollected(userId, articleId);
        return ApiResponse.success(result);
    }

    /**
     * 获取文章互动统计数据
     */
    @GetMapping("/stats/{articleId}")
    public ApiResponse<InteractionService.InteractionStatistic> getArticleStats(@PathVariable Long articleId) {
        InteractionService.InteractionStatistic stats = interactionService.getArticleInteractionStats(articleId);
        return ApiResponse.success(stats);
    }
} 